'''
Created on 2011-07-17

@author: Mikael
'''

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from Model import Session
from Model.TreeModel import TreeModel
from Model.GenericNode import GenericNode
from Model.NodeFolder import NodeFolder
from Model.Resource import ResourceType, Resource
from Model.NodeResource import NodeResource

class ProjectExplorerController(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        session = Session.getSessionInstance()
        self.mProject = session.getProject()
        
    def deleteNodes(self, qmodelIndexList):
        '''
        '''
        model   = self.mProject.getTreeModel()
        
        for QModelIndex in qmodelIndexList:
            node = QModelIndex.internalPointer()
            model.removeRow(node.getChildIndexInParentNode(), model.parent(QModelIndex))
            
    def addFolder(self, name, QModelIndex):
        node = NodeFolder(name)
        model = self.mProject.getTreeModel()
        nodeIdxInParent = 0
        parentNode = QModelIndex.internalPointer()
        
        # A folder can only be aded to an another folder
        if type ( parentNode ) is not NodeFolder:
            QModelIndex = model.parent(QModelIndex)
            oldParent = parentNode
            parentNode = QModelIndex.internalPointer()
            nodeIdxInParent = parentNode.getIndexOf(oldParent)

        TreeModel.insertRow(model, nodeIdxInParent, QModelIndex, node)
    
    def addResource(self, filePath, QModelIndex):
        node = NodeResource(Resource(ResourceType.Material, "/Dossier1/ImportedMaterial") );
        model = self.mProject.getTreeModel()
        nodeIdxInParent = 0
        parentNode = QModelIndex.internalPointer()
        
        # A folder can only be aded to an another folder
        if type ( parentNode ) is not NodeFolder:
            QModelIndex = model.parent(QModelIndex)
            oldParent = parentNode
            parentNode = QModelIndex.internalPointer()
            nodeIdxInParent = parentNode.getIndexOf(oldParent)

        TreeModel.insertRow(model, nodeIdxInParent, QModelIndex, node)
        
        